package com.niit.repository;

import com.niit.entity.Info;
import com.niit.entity.User;
import jakarta.persistence.criteria.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface InfoRepository extends JpaRepository<Info, Integer>, JpaSpecificationExecutor<Info> {
    Info findInfoByPhoneAndPassword(String phone, String password);
    Info findInfoByPhone(String phone);

    class InfoSpecifications {
        public static Specification<Info> withDynamicQuery(
                String phone, String username, String realName, String gender) {

            return (Root<Info> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                Predicate predicate = cb.conjunction();

                if (phone != null) predicate = cb.and(predicate, cb.equal(root.get("phone"), phone));
                if (username != null) predicate = cb.and(predicate, cb.equal(root.get("username"), username));
                if (realName != null) predicate = cb.and(predicate, cb.equal(root.get("realName"), realName));
                if (gender != null) predicate = cb.and(predicate, cb.equal(root.get("gender"), gender));
                return predicate;
            };
        }
    }
}
